// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// d [author] rhyme_lph
// A [email]  rhymelph@gmail.com
// P [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/page/dashboard/sales/views/sales_invoice_view.dart';
import 'package:finance_app/src/page/dashboard/sales/views/sales_quotation_view.dart';
import 'package:finance_app/src/page/dashboard/sales/views/sales_summary_view.dart';
import 'package:finance_app/src/utils/permission_utils.dart';
import 'package:finance_app/src/widgets/button/assistant_button.dart';
import 'package:finance_app/src/widgets/button/normal_float_svg_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/common_tab_bar.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/dashboard/sales/sales_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';

@RouterMeta(paramName: "salesPage")
class SalesPage extends StatefulWidget {
  final int initIndex;
  final String initFilter;
  const SalesPage({Key key, this.initIndex, this.initFilter}) : super(key: key);

  @override
  _SalesPageState createState() => _SalesPageState();
}

class _SalesPageState extends State<SalesPage>
    with PagePresenter<SalesPage, SalesProvider>, TickerProviderStateMixin {
  TabController controller;

  @override
  void initState() {
    super.initState();
    provider.initFilter = widget.initFilter;
    provider.currentIndex = widget.initIndex ?? 0;
    controller = TabController(
        length: provider.tabList.length,
        vsync: this,
        initialIndex: widget.initIndex ?? 0);
    provider.tabController = controller;
    controller.addListener(() {
      provider?.onChangeTab(controller);
    });
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
  }

  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: S.current.sales, actions: [
        AssistantButton(
          entryPoint: "Sales",
        ),
      ]);

  @override
  Widget get buildWidget => listBuilder<bool>(
        selector: (_, __) => __.buttonBarIsUp,
        builder: (BuildContext context, bool value, Widget child) => Stack(
          children: [
            child,
            _buildBottomButton(value),
            // _buildTop(),
          ],
        ),
        child: _buildRecordList(),
      );

  Widget _buildBottomButton(bool isTop) => AnimatedPositioned(
      duration: Duration(milliseconds: 300),
      left: 0.0,
      right: 0.0,
      bottom: isTop
          ? MediaQuery.of(context).padding.bottom + getDp(40)
          : -getDp(60),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          NormalFloatSvgButton(
            onTap: provider.onNavAdd,
            asset: A.assetsImagesDashboardAdd,
          ),
        ],
      ));

  Widget _buildRecordList() => ColoredBox(
        color: Colors.white,
        child: Column(
          children: [
            _buildTabBar(),
            Expanded(
                child: TabBarView(
                    controller: controller, children: _buildTabView())),
          ],
        ),
      );

  List<Widget> _buildTabView() => <Widget>[
        SalesSummaryView(),
        if (Config.hasPermission(PermissionUtils.quotation))
          SalesQuotationView(),
        if (Config.hasPermission(PermissionUtils.invoice)) SalesInvoiceView(),
      ];

  Widget _buildTabBar() => CommonTabBar(
        controller: controller,
        // isScrollable: true,
        tabs: provider.tabList
            .map((e) => MyTab(
                  text: '$e',
                ))
            .toList(),
      );
}
